<template>
  <div class="myContainer">
    <!-- 头部标题 start -->  
    <el-row class="myBorderBottom">
      <el-col :span="7" class="myTitleText myLineHeight_40px">
        {{title}}
      </el-col>
      <el-col :span="17" class="myTextAlignRight">
        <el-button type="primary" class="myBgGreen" @click="add">新增</el-button>
      </el-col>
    </el-row>
    <!-- 头部标题 end -->
    <el-row :gutter="10" class="myPaddingTop_10px"> 
      <!-- <el-col :span="4">
        <template>
          <el-select v-model="typeSelected"
            clearable
            placeholder="全部计划类型">
            <el-option
              v-for="item in type"
              :key="item.label"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </template>
      </el-col>
      <el-col :span="6">
        <el-date-picker
          v-model="time"
          type="daterange"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-col> -->
      <el-col :span="6">
        <el-input placeholder="请输入关键词" v-model="kw"></el-input>
      </el-col>
      <el-col :span="3">
        <el-button type="primary" @click="search">查询</el-button>
      </el-col>
    </el-row>
    
    <el-row class="myPaddingTop_10px">
      <el-col :span="24">
        <template>
          <el-table
            border
            :data="list"
            :stripe = true
            v-loading="loading"
            style="width: 100%">
            <el-table-column
              label="序号"
              type="index"
              width="100">
            </el-table-column>
            <el-table-column
              label="例行计划名称"
              prop="plan_title">
            </el-table-column>
            <el-table-column
              label="监察人职务"
              prop="supervisorrole_name">
            </el-table-column>
            <el-table-column
              label="计划周期"
              prop="plan_cycle">
            </el-table-column>
            <el-table-column
              label="计划频次"
              prop="frequency">
            </el-table-column>
            <el-table-column
              label="状态"
              prop="status">
              <template scope="scope">
                <span v-if="scope.row.status==0">未完成</span>
                <span v-if="scope.row.status==1">已完成</span>
              </template>
            </el-table-column>
            <el-table-column
              label="备注">
            </el-table-column>
            <!-- <el-table-column
              label="创建时间"
              prop="create_time">
            </el-table-column>
            <el-table-column
              label="计划名称"
              prop="plan_title">
            </el-table-column>
            <el-table-column
              label="计划类型"
              prop="gen_type">
              <template scope="scope">
                <span v-if="scope.row.gen_type==1">周期性</span>
                <span v-else>临时性</span>
              </template>
            </el-table-column> 
            <el-table-column
              label="创建人"
              prop="creator_name">
            </el-table-column>
            <el-table-column
              label="初次生效日期"
              prop="first_date">
            </el-table-column>
            <el-table-column
              label="下次生效时间"
              prop="next_date">
            </el-table-column> -->
            <el-table-column
              label="功能操作"
              width="200">
              <template scope="scope">
                <el-button class="look" type="text" size="small" @click="info(scope.row.plan_id)">详情</el-button> |
                <span v-if="scope.row.editable">
                  <el-button class="look" type="text" size="small" @click="update(scope.row.plan_id)">编辑</el-button> |
                </span>
                <!-- <el-button class="look" type="text" size="small" @click="create(scope.row.plan_id,scope.row.plan_title)">立即创建</el-button> | -->
                <el-button class="look" type="text" size="small" @click="ondelete(scope.row.plan_id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </template>
      </el-col>
    </el-row>
    
    <div class="block">                    
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page_num"
        :page-sizes="[10, 15]"
        :page-size="page_size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>

  </div>
</template>     
  
<style scoped>
  .myBorderBottom{
    border-bottom:1px solid #dbdbdb;
    padding-bottom: 5px;
  }
  .myTextAlignRight{
    text-align: right;
  }
  .myContainer{
    background: #fff;
    padding: 10px;
  }
  .myTitleText{
    font-weight: 500;
    font-size: 16px;
  }
  .myLineHeight_40px{
    line-height: 40px;
  }
  .myPaddingTop_10px{
    padding-top: 10px;
  }  
  .myred{
    color: #f00;
    font-size: 12px;
  }
  .myMarginBottom_10px{
    margin-bottom: 10px;
  }
  .myLeftMsg{
    line-height: 40px;
    text-align: right;
  } 
</style>

<script>
  import { getRegion } from '@/utils/localstorage';
  import { getPlanList , deletePlan , createTask } from "@/api/quality";
  export default {
    data(){
      return{
        title:"例行计划管理",
        page_num:null,
        page_size:null,
        total:null,
        list:[],
        loading:false,
        regionOptions: [],
        regionOptSelected: [],
        taskSelected:"",
        task:[
          {label:"任务类型1",value:"1"},
          {label:"任务类型2",value:"2"}
        ],
        typeSelected:"",
        type:[
          {label:"周期性",value:"1"},
          {label:"临时性",value:"0"}
        ],
        time:[],
        kw:""
      }
    },
    components: {

    },
    created(){
      document.title=this.title;
      this.ongetPlanList();
    },
    computed: {
      
    },
    filters:{ 
    },
    watch: {
      
    },
    methods:{
      handleSizeChange(val) {//每页条数控制
        this.page_size = val;
        this.ongetPlanList();
      },
      handleCurrentChange(val) {//每页
        let vm=this;
        let monitoringPlanConfig=window.sessionStorage.getItem("monitoringPlanConfig");
        if(monitoringPlanConfig){
          let config=JSON.parse(monitoringPlanConfig);
          // vm.typeSelected=config.typeSelected;
          vm.kw=config.keyword;
          vm.time=config.time;
          vm.page_num = Number(config.page_num);
          window.sessionStorage.removeItem("monitoringPlanConfig");
        }else{
          vm.page_num = val;
        }
        vm.ongetPlanList();
      },
      search(){
        this.ongetPlanList();
      },
      add(){
        this.$router.push({
          path:"addPlanList",
          query:{
            type:"add"
          }
        })
      },
      info(id){
        let vm=this;
        let monitoringPlanConfig={
          typeSelected:1,
          keyword:vm.kw,
          time:vm.time,
          page_num:vm.page_num,
        }
        window.sessionStorage.setItem("monitoringPlanConfig",JSON.stringify(monitoringPlanConfig));
        vm.$router.push({
          path:'planlistMGTComonents',//"monitoringPlanComonents",
          query:{
            type:"info",
            id:id
          }
        })
      },
      update(id){
        let vm=this;
        let monitoringPlanConfig={
          // typeSelected:vm.typeSelected,
          keyword:vm.kw,
          time:vm.time,
          page_num:vm.page_num,
        }
        window.sessionStorage.setItem("monitoringPlanConfig",JSON.stringify(monitoringPlanConfig));
        vm.$router.push({
          path:'planlistMGTComonents',//"monitoringPlanComonents",
          query:{
            type:"update",
            id:id
          }
        })
      },
      ondelete(id){
        let vm=this; 
        vm.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deletePlan(id).then(response => {
            vm.$message.success("执行成功！");
            vm.ongetPlanList();
          }).catch(error => {
            vm.$message.error("执行失败！"+error.response.data.error_msg);
          })
        }).catch(() => {
          vm.$message({
            type: 'info',
            message: '已取消删除'
          });          
        }); 
      },
      ongetPlanList(){
        let vm=this;
        let params={
          gen_type:1,
          keyword:vm.kw,
          start_time:vm.time?vm.time[0]:'',
          end_time:vm.time?vm.time[1]:'',
          page_num:vm.page_num,
          page_size:vm.page_size
        }
        vm.loading=true;
        getPlanList(params).then(response => {
          let res=response.data;
          vm.list=res.rows;
          vm.page_size=res.page_size;
          vm.page_num=res.page_num;
          vm.total=res.total;
          vm.loading=false;
        }).catch(error => {
          vm.loading=false;
          vm.$message.error("执行失败！"+error.response.data.error_msg);
        })
      },
      create(id,title){
        this.$confirm(`是否立即创建 [ ${title} ] 内的任务？`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          createTask(id).then(response => {
            let res=response.data;
            this.$message.success("执行成功！");
          }).catch(error => {
            this.$message.error("执行失败！"+error.response.data.error_msg);
          })
        }).catch(() => {});
      }

    }
}
</script>

